/**
 * long description for the file
 *
 * @summary 采集接口
 * @author wu[308822989@qq.com]
 *
 * Created at     : 2018-12-18 14:15:37 
 * Last modified  : 2018-12-19 15:23:24
 */

const Base = require('./base.js');
const http = require('http');
const iconv = require('iconv-lite');
const lodash = require('lodash');

module.exports = class extends Base {
  async indexAction() {
    return this.success(true, '成功');
  }

  async testAction() {
    if (!this.isPost) {
      return this.fail('请求错误');
    }
    //
    let data = this.post();
    let { type } = this.post();
    //  拆分数组去前后空格
    let urlArr = lodash.map( lodash.split(data.source_aurl, '\n') , function (n) {
      return lodash.trim(n);
    });
    //  去空数据
    urlArr = lodash.remove(urlArr, function (n) {
      return !!n;
    });
    //
    for (let url of urlArr) {
      let html = await this.getHtml(url);
      //  链接补全
      html = this.htmlSetHref(html, url);
      //
      if (type === 'source_matchlist') {
        // 列表链接正则
        // let exp = new RegExp(data.source_matchlist, 'gm');
        // html.match(exp);
        let listBody = this.getList(html, data.source_matchlist);
        let listArr = this.getHref(listBody, data.source_matchurl);
        //  查询第一个链接
        if (listArr.length > 0) {
          let oneHtml = await this.getHtml(listArr[0]);
          //  查询标题
          //  查询内容
        }
      }
    }

    console.log(data);


    return this.success(true, '成功');
  }

  /**
   * 取列表区域文本
   * @param {String} htmlStr 
   * @param {*} expStr 
   */
  getList(htmlStr, expStr) {
    let exp = new RegExp(expStr, 'gm');
    let str = htmlStr.match(exp);
    return (str && str.length > 0) ? str[0] : '';
  }

  //定义函数
  getHref(htmlStr, expStr) {
    //  查询链接顺序
    var reg = new RegExp(expStr, 'gm');
    var arr = [];
    var tem = null;
    while(tem = reg.exec(htmlStr)){
        arr.push(tem[1]);
    }
    return arr;
  }


  /**
   * html url 补全
   * @param {*} html 
   * @param {*} url 
   */
  htmlSetHref (html, url) {
    //  网址链接
    let origin = url.match(/(\w+:\/\/.*\.\w+)\//);
    origin = origin.length > 0 ? origin[1] : '';
    let act = url.match(/\w+:\/\/.*\..*\//);
    act = act.length > 0 ? act[0] : '';
    //
    if (html && html.length > 0) {
      let hrefArr = html.match(new RegExp('href="(.*)"'));
      let srcArr = html.match(new RegExp('src="(.*)"'));
      for (let str of hrefArr) {
        let ostr = str;
        let ih = str;
        if (/^https*/.test(str)) {
          //  
        } else if (/^\//.test(str)) {
          //  /
          ih = origin + str;
        } else if (/^\.\.\//.test(str)) {
          //  ../ 个数
          let len = str.match(/\.\.\//g);
          len = len ? len.length : 0;
          let temp = act.substr(origin.length - 1);
          temp = temp.split('/');
          if (temp.length >= len) {
            //  
            temp.reverse();
            for (let i = 0; i < len; i++) {
              str.replace(/\.\.\//, temp[i]);
            }
            ih = origin + '/' + str;
          } else {
            str.replace(/\.\.\//, '');
            ih = origin + '/' + str;
          }
        } else if (/^[^https|http|\/|\.\.\/]/.test(str)) {
          ih = act + str;
        }
        html = html.replace(ostr, ih);
      }
      for (let str of srcArr) {
        let ostr = str;
        let ih = str;
        if (/^https*/.test(str)) {
          //  
        } else if (/^\//.test(str)) {
          //  /
          ih = origin + str;
        } else if (/^\.\.\//.test(str)) {
          //  ../ 个数
          let len = str.match(/\.\.\//g);
          len = len ? len.length : 0;
          let temp = act.substr(origin.length - 1);
          temp = temp.split('/');
          if (temp.length >= len) {
            //  
            temp.reverse();
            for (let i = 0; i < len; i++) {
              str.replace(/\.\.\//, temp[i]);
            }
            ih = origin + '/' + str;
          } else {
            str.replace(/\.\.\//, '');
            ih = origin + '/' + str;
          }
        } else if (/^[^https|http|\/|\.\.\/]/.test(str)) {
          ih = act + str;
        }
        html = html.replace(ostr, ih);
      }
    }
    return html;
  }

  /**
   * 采集源码
   */
  async getHtml (url, charset = 'utf8') {
    return new Promise((resolve, reject) => {
      http.get(url, function(res){
          var length=0;
          var arr=[];
          res.on("data",function(chunk){
              arr.push(chunk);
              length+=chunk.length;
          });   
          res.on("end",function(){
              var data=Buffer.concat(arr,length);
              var change_data = iconv.decode(data, charset); 
              if (change_data.length > 0) {
                resolve(change_data.toString());
              } else {
                resolve('');
              }
          });
      });
    });

  }


};
